var timeline_lock = false;
var feature = 0;
var user_id = 0;
var mention = 0;
var comments_by_me = 0;
var comments_to_me = 0;
var followers = 0;
var friends = 0;


function load_timeline(max_id , do_loading) {
  if (timeline_lock) {
    return false;
  }
  
  var add = 'feature='+feature;
  if (max_id) {
    add += '&max_id='+max_id;
  }
  if (user_id) {
    add += '&user_id='+user_id;
  }
  if (mention) {
    add += '&mention=1';
  }
  if (comments_by_me) {
    add += '&comments_by_me=1';
  }
  if (comments_to_me) {
    add += '&comments_to_me=1';
  }
  if (followers) {
	add += '&followers=1';
  }
  if (friends) {
	add += '&friends=1';
  }

  timeline_lock = true;
  
  if (do_loading) {
    loading();
  }

  $('#weibo_bag').append('<div class="W_loading" node-type="lazyload"><span>正在加载中，请稍候...</span></div>');
  
  $.get(SITE_URL+'/ajax/timeline.php?'+add, function(html){
    if (html == '') {
        var list = $('dl[action-type=feed_list_item]');
        var pos = $('dl[action-type=feed_list_item]').length - 1;
        var max_id = parseInt($('dl[action-type=feed_list_item]:eq('+pos+')').attr('mid'));
        if (max_id) {
            $('dl[action-type=feed_list_item]:eq('+pos+')').attr('mid', 0);
        }
    } else {
        $('#weibo_bag').append(html);
    }

    if (do_loading) {
      loading('off');
    }
    $('.W_loading').remove();
    timeline_lock = false;
    
  });
}

$.fn.rotate = function(p){

	var img = $(this)[0],
		n = img.getAttribute('step');
	// 保存图片大小数据
	if (!this.data('width') && !$(this).data('height')) {
		this.data('width', img.width);
		this.data('height', img.height);
	};
	this.data('maxWidth',img.getAttribute('maxWidth'))

	if(n == null) n = 0;
	if(p == 'left'){
		(n == 0)? n = 3 : n--;
	}else if(p == 'right'){
		(n == 3) ? n = 0 : n++;
	};
	img.setAttribute('step', n);

	// IE浏览器使用滤镜旋转
	if(document.all) {
		if(this.data('height')>this.data('maxWidth') && (n==1 || n==3) ){
			if(!this.data('zoomheight')){
				this.data('zoomwidth',this.data('maxWidth'));
				this.data('zoomheight',(this.data('maxWidth')/this.data('height'))*this.data('width'));
			}
			img.height = this.data('zoomwidth');
			img.width  = this.data('zoomheight');

		}else{
			img.height = this.data('height');
			img.width  = this.data('width');
		}

		img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ n +')';
		// IE8高度设置
		if ($.browser.version == 8) {
			switch(n){
				case 0:
					this.parent().height('');
					//this.height(this.data('height'));
					break;
				case 1:
					this.parent().height(this.data('width') + 10);
					//this.height(this.data('width'));
					break;
				case 2:
					this.parent().height('');
					//this.height(this.data('height'));
					break;
				case 3:
					this.parent().height(this.data('width') + 10);
					//this.height(this.data('width'));
					break;
			};
		};
	// 对现代浏览器写入HTML5的元素进行旋转： canvas
	}else{
		var c = this.next('canvas')[0];
		if(this.next('canvas').length == 0){
			this.css({
			          'visibility': 'hidden', 'position': 'absolute'
			          });
			c = document.createElement('canvas');
			c.setAttribute('class', 'maxImg canvas');
			img.parentNode.appendChild(c);
		}
		var canvasContext = c.getContext('2d');
		switch(n) {
			default :
			case 0 :
				img.setAttribute('height',this.data('height'));
				img.setAttribute('width',this.data('width'));
				c.setAttribute('width', img.width);
				c.setAttribute('height', img.height);
				canvasContext.rotate(0 * Math.PI / 180);
				canvasContext.drawImage(img, 0, 0);
				break;
			case 1 :
				if(img.height>this.data('maxWidth') ){
					h = this.data('maxWidth');
					w = (this.data('maxWidth')/img.height)*img.width;
				}else{
					h = this.data('height');
					w = this.data('width');
				}
				c.setAttribute('width', h);
				c.setAttribute('height', w);
				canvasContext.rotate(90 * Math.PI / 180);
				canvasContext.drawImage(img, 0, -h, w ,h );
				break;
			case 2 :
				img.setAttribute('height',this.data('height'));
				img.setAttribute('width',this.data('width'));
				c.setAttribute('width', img.width);
				c.setAttribute('height', img.height);
				canvasContext.rotate(180 * Math.PI / 180);
				canvasContext.drawImage(img, -img.width, -img.height);
				break;
			case 3 :
				if(img.height>this.data('maxWidth') ){
					h = this.data('maxWidth');
					w = (this.data('maxWidth')/img.height)*img.width;
				}else{
					h = this.data('height');
					w = this.data('width');
				}
				c.setAttribute('width', h);
				c.setAttribute('height', w);
				canvasContext.rotate(270 * Math.PI / 180);
				canvasContext.drawImage(img, -w, 0,w,h);
				break;
		};
	};
};



$('.bigcursor').live('click', function(){
  var feed_list = $(this).parents('dl').find('*[node-type=feed_list_media_disp]');
  
  var self = $(this);
  self.next().show();
  var pic = self.attr('src').replace('thumbnail', 'bmiddle');
  var img = $('<img />');
  img.attr('src', pic);
  img.load(function(){
    self.next().hide();
    self.parents('*[node-type=feed_list_media_prev]').hide();
    
    feed_list.show();

    if (feed_list.is('dl')) {
      feed_list.append('<dd class="expand"></dd>');
      feed_list = feed_list.find('dd');
    }
      feed_list.html('<p class="W_no_border">\
          <a class="retract" href="javascript:void(0);" action-type="feed_list_media_toSmall">收起</a><i class="W_vline">|</i>\
          <a class="show_big" target="_blank" href="'+self.attr('src').replace('thumbnail', 'large')+'">查看大图</a><i class="W_vline">|</i>\
          <a class="turn_left" href="javascript:void(0);" action-type="feed_list_media_toLeft">向左转</a><i class="W_vline">|</i>\
          <a class="turn_right" href="javascript:void(0);" action-type="feed_list_media_toRight">向右转</a>\
        </p>\
        <div action-type="feed_list_media_bigimgDiv" class="smallcursor">\
          <img width="440" maxWidth="440" src="'+pic+'" action-type="feed_list_media_bigimg" />\
        </div>').find('.retract').click(function(){
        $(this).parents('dl').find('*[node-type=feed_list_media_disp]').empty().hide();
        self.parents('*[node-type=feed_list_media_prev]').show();
      });
      
      feed_list.find('.smallcursor').click(function(){
        feed_list.find('.retract').click();
      });
      feed_list.find('.turn_left').click(function(){
        $('img[action-type=feed_list_media_bigimg]', feed_list).rotate('left');
      });
      feed_list.find('.turn_right').click(function(){
        $('img[action-type=feed_list_media_bigimg]', feed_list).rotate('right');
      });

    
    
  });


  
});